package com.runchamp;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONObject;

/* Activity in charge of showing the user's achievments in the app: rating, num of races, etc..)
 */
public class AchievementsActivity extends Fragment {
    private static final String path = "http://runtobo.appspot.com/getAchievements";
    private TextView BestScoreTextView;
    private TextView BestPlaceTextView;
    private TextView TotalDistanceTextView;
    private TextView LastRunTextView;
    private TextView AbsolutePositionTextView;
    private TextView AverageSpeedTextView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final SharedPreferences prefs = getActivity().getSharedPreferences("MyPrefsFile", Context.MODE_PRIVATE);
        View rootView = inflater.inflate(R.layout.activity_achievements, container, false);
        BestScoreTextView = (TextView) rootView.findViewById(R.id.Score);
        BestPlaceTextView = (TextView) rootView.findViewById(R.id.BestPlace);
        TotalDistanceTextView = (TextView) rootView.findViewById(R.id.TotalDistance);
        LastRunTextView = (TextView) rootView.findViewById(R.id.LastRun);
        AbsolutePositionTextView = (TextView) rootView.findViewById(R.id.AbsolutePosition);
        AverageSpeedTextView = (TextView) rootView.findViewById(R.id.avg_speed_value);
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.add("userId", prefs.getString("userID", ""));
        client.post(path, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(JSONObject response) {
                String score = response.optString("score");
                String distance = response.optString("totalDistance");
                String lastRun = response.optString("lastRun");
                String bestPlace = response.optString("bestPlace");
                String absPosition = response.optString("absolutePosition");
                String avgSpeed = response.optString("averageSpeed");

                BestScoreTextView.setText(score);
                TotalDistanceTextView.setText(String.format("%.2f", Float.parseFloat(distance)));
                LastRunTextView.setText(lastRun);
                BestPlaceTextView.setText(bestPlace);
                AbsolutePositionTextView.setText(absPosition);
                AverageSpeedTextView.setText(String.format("%.2f", Float.parseFloat(avgSpeed)));
            }
        });
        return rootView;
    }

}
